from fastapi import Response
from fastapi.responses import HTMLResponse
from .status_manager import status_manager

def get_html_content(status):
    return f"""
<!DOCTYPE html>
<html>
<head>
    <title>系统运行状态</title>
    <meta charset="utf-8">
    <style>
        body {{ font-family: Arial, sans-serif; margin: 20px; }}
        .container {{ max-width: 800px; margin: 0 auto; }}
        .status-box {{ 
            border: 1px solid #ddd; 
            padding: 15px; 
            margin: 10px 0; 
            border-radius: 5px;
        }}
        .error {{ color: red; }}
        .count {{ font-size: 24px; font-weight: bold; }}
        .refresh-time {{ color: #666; font-size: 12px; }}
    </style>
    <script>
        function refreshPage() {{
            location.reload();
        }}
        // 每30秒自动刷新
        setInterval(refreshPage, 30000);
    </script>
</head>
<body>
    <div class="container">
        <h1>系统运行状态监控</h1>
        <p class="refresh-time">日期: {status['date']} (每30秒自动刷新)</p>
        
        <div class="status-box">
            <h2>内容获取</h2>
            <p class="count">今日处理: {status['stats']['content_fetch']['count']} 条</p>
            {f'''<p class="error">
                最近错误 ({status['stats']['content_fetch']['error_time'].strftime('%H:%M:%S')}): 
                {status['stats']['content_fetch']['last_error']}
            </p>''' if status['stats']['content_fetch']['last_error'] else ''}
        </div>

        <div class="status-box">
            <h2>LLM判断</h2>
            <p class="count">今日处理: {status['stats']['llm_judge']['count']} 条</p>
            {f'''<p class="error">
                最近错误 ({status['stats']['llm_judge']['error_time'].strftime('%H:%M:%S')}): 
                {status['stats']['llm_judge']['last_error']}
            </p>''' if status['stats']['llm_judge']['last_error'] else ''}
        </div>

        <div class="status-box">
            <h2>语音生成</h2>
            <p class="count">今日处理: {status['stats']['tts_generation']['count']} 条</p>
            {f'''<p class="error">
                最近错误 ({status['stats']['tts_generation']['error_time'].strftime('%H:%M:%S')}): 
                {status['stats']['tts_generation']['last_error']}
            </p>''' if status['stats']['tts_generation']['last_error'] else ''}
        </div>
    </div>
</body>
</html>
"""

def get_monitor_page():
    """获取监控页面HTML"""
    status = status_manager.get_status()
    html_content = get_html_content(status)
    return HTMLResponse(content=html_content) 